//
//  LESelectStaffController.m
//  LoveExpressBusiness
//
//  Created by 刘甲奇 on 2017/5/17.
//  Copyright © 2017年 DFH. All rights reserved.
//

#import "LESelectStaffController.h"

@interface LESelectStaffController ()
/**
 service_id
 */
@property(nonatomic,copy)NSString *service_id;

@end

@implementation LESelectStaffController

- (void)setupUI
{
    [super setupUI];
    self.title = @"请选择工作人员";
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(sureClick)];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"haha"];
      
    self.navigationItem.rightBarButtonItem = rightItem;
    [self initHeader];
}
- (void)loadData
{
    [self requestRefreshData];
}

#pragma mark- 网络请求
- (void)requestRefreshData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:self.order_id forKey:@"order_id"];
    [dict setObject:@"1000" forKey:@"page"];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
     __weak typeof (self) weakSelf = self;
    [API requestAppendKey:StoreOrderStaffsUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        NSDictionary *datas = successCode[@"datas"];
        NSString *error = datas[@"error"];
        NSString *totalPage = successCode[@"page_total"];
        weakSelf.totalPage = totalPage;
        if (error)
        {
            [SVProgressHUD showErrorWithStatus:error];
        }else
        {
            //服务人员数组
            NSArray *free_service = successCode[@"free_service"];
            if (free_service.count == 0)
            {
                [weakSelf addNoDataViewWithTipText:@"没有在线服务人员"];
            }else
            {
                [weakSelf hiddenNoDataView];
                [weakSelf.dataSource removeAllObjects];
                [weakSelf.dataSource addObjectsFromArray:free_service];
                [weakSelf.tableView reloadData];
            }

        }
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}
- (void)requestAssignStaff
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:self.order_id forKey:@"order_id"];
    [dict setObject:self.service_id forKey:@"service_id"];
    __weak typeof (self) weakSelf = self;
    [SVProgressHUD showWithStatus:@"正在加载中"];
    [API requestAppendKey:StoreOrderAssignStaffsUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        if ([datas isKindOfClass:[NSString class]]) {
            [SVProgressHUD showInfoWithStatus:datas];
            
        }
        if ([datas isKindOfClass:[NSDictionary class]]) {
            NSDictionary *data = (NSDictionary *)datas;
            NSString *error = data[@"error"];
            [SVProgressHUD showInfoWithStatus:error];
        }
        [weakSelf.navigationController popViewControllerAnimated:YES];
        

    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];

}

#pragma mark- tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"haha" forIndexPath:indexPath];
    NSDictionary *dict = self.dataSource[indexPath.row];
    cell.textLabel.text = dict[@"service_truename"];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = self.dataSource[indexPath.row];
    self.service_id = dict[@"service_id"];
}
#pragma mark- 点击事件
- (void)sureClick
{
    LELog(@"%s---%d---%@",__func__,__LINE__,self.service_id);
    if (self.service_id) {
        [self requestAssignStaff];
    }else{
        [SVProgressHUD showInfoWithStatus:@"没有选择服务人员"];
    }
    
}


@end






